package com.heima.solr;

import java.io.IOException;
import java.util.List;

import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrQuery.ORDER;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrServer;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.SolrInputDocument;
import org.junit.Test;

import com.heima.bean.Item;

public class SolrTest {

	@Test
	public void creatIndex() throws SolrServerException, IOException{
		
		//连接服务器
		HttpSolrServer server = new HttpSolrServer("http://localhost:8080/solr/core");
		//创建solr的输入的document
		SolrInputDocument document = new SolrInputDocument();
		//添加字段
		document.addField("id", "30");
		document.addField("title", "丰田汽车，一代神车");
		document.addField("price", 9999999);
		
		//添加文档到server
		server.add(document);
		//提交数据
		server.commit();
	}
	
	@Test
	public void creatIndex2() throws SolrServerException, IOException{
		
		//连接服务器
		HttpSolrServer server = new HttpSolrServer("http://localhost:8080/solr/core");
	
		Item item = new Item();
		item.setId("31");
		item.setTitle("这是一个键盘");
		item.setPrice(898898);
		
		//将对象添加到server
		server.addBean(item);
		server.commit();
	}
	
	@Test
	public void deleteIndex2() throws SolrServerException, IOException{
		
		//连接服务器
		HttpSolrServer server = new HttpSolrServer("http://localhost:8080/solr/core");
	
		server.deleteById("31");
		server.commit();
	}
	
	@Test
	public void queryDocumentIndex2() throws SolrServerException, IOException{
		
		//连接服务器
		HttpSolrServer server = new HttpSolrServer("http://localhost:8080/solr/core");
	
		SolrQuery query = new SolrQuery("title:iphone6s");
		QueryResponse response = server.query(query);
		
		SolrDocumentList results = response.getResults();
		int size = results.size();
		System.out.println("查询到的最大记录数："+size);
		
		for (SolrDocument document : results) {
			System.out.println("id:"+document.getFieldValue("id"));
			System.out.println("title:"+document.getFieldValue("title"));
			System.out.println("price:"+document.getFieldValue("price"));
		}
//		server.commit();
	}
	@Test
	public void queryDocumentBean() throws SolrServerException, IOException{
		
		//连接服务器
		HttpSolrServer server = new HttpSolrServer("http://localhost:8080/solr/core");
		
		SolrQuery query = new SolrQuery("title:手机");
		// 设置查询的排序参数,参数： 排序的字段名、排序方式
		query.setSort("price",ORDER.asc);
		QueryResponse response = server.query(query);
		
		List<Item> list = response.getBeans(Item.class);
		
		System.out.println("最大记录数:"+list.size());
		
		for (Item item : list) {
			System.out.println(item);
		}
	}
	
//	@Test
//	public void queryDocumentBean2() throws SolrServerException, IOException{
//		
//		//连接服务器
//		HttpSolrServer server = new HttpSolrServer("http://localhost:8080/solr/core");
//		
//		String query="*:*";
//		server.deleteByQuery(query);
//		server.commit();
//	}
}
